package com.supplychain.scm.core.common.exception;

import com.supplychain.scm.core.common.annotation.EnableException;
import org.springframework.aop.config.AopConfigUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Objects;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName ExceptionInterceptorRegistrar
 * @createTime 17:48:00 2022/12/03
 */
public class ExceptionInterceptorRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
        AnnotationAttributes enableDbySupplyInterceptor = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableException.class.getName()));
        if(Objects.nonNull(enableDbySupplyInterceptor)) {
            Class<? extends CustomException> clazz = enableDbySupplyInterceptor.getClass("exceptionClass");
            CustomAssert.setClazz(clazz);
        }
    }
}
